package com.cxs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxs.entity.dto.BasePageDTO;
import com.cxs.entity.Student;
import com.cxs.service.ClazzService;
import com.cxs.service.StudentService;
import com.cxs.entity.bo.ClazzBo;
import com.cxs.entity.vo.LayuiPageVO;
import com.cxs.entity.vo.Result;
import com.cxs.entity.vo.StudentLoginVO;
import com.cxs.entity.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/*
 * @Project:seat-subscribe-sys
 * @Author:cxs
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClazzService clazzService;

    @RequestMapping("/")
    public String toIndex(HttpSession session, Model model) {
        Object user = session.getAttribute("loginUser");
        if (null != user) {
            if (user instanceof StudentLoginVO) {
                StudentLoginVO loginVO = (StudentLoginVO) user;
                model.addAttribute("user", loginVO);
                return "student/index";
            }else {
                return "redirect:/auth/login";
            }
        } else {
            return "redirect:/auth/login";
        }
    }

    @RequestMapping("/toStudentList")
    public String toStudentList(HttpSession session, Model model) {
        return "admin/studentList";
    }

    @RequestMapping("/toMyInfo")
    public String toMyInfo(HttpSession session, Model model) {
        Object user = session.getAttribute("loginUser");
        StudentLoginVO loginVO = (StudentLoginVO) user;
        Student stu = studentService.getById(loginVO.getSno());
        ClazzBo clazzBo = clazzService.getClazzInfoByClazzId(stu.getClazzId());
        model.addAttribute("user", stu);
        model.addAttribute("clazz", clazzBo);
        return "student/myInfo";
    }


    @RequestMapping("/getStudentList")
    @ResponseBody
    public LayuiPageVO toStudentList(BasePageDTO dto, HttpSession session, Model model) {
        LayuiPageVO vo = new LayuiPageVO();
        IPage<Student> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(dto.getKeyword())) {
            queryWrapper.like(Student::getName, dto.getKeyword())
                    .or().like(Student::getSno, dto.getKeyword());
        }
        List<StudentVo> voList = studentService.getListByKeyword(dto.getKeyword());
        vo.setCode(0);
        vo.setCount(page.getTotal());
        vo.setMsg("ok");
        vo.setData(voList);
        return vo;
    }

    @RequestMapping("/remove")
    @ResponseBody
    public Result remove(Integer id, HttpSession session, Model model) {
        Result vo = new Result(200, "OK");
        studentService.removeById(id);
        return vo;
    }

    @RequestMapping("/updateInfo")
    @ResponseBody
    public Result updateInfo(Student student, HttpSession session, Model model) {
        Result vo = new Result(200, "OK");
        studentService.updateById(student);
        return vo;
    }
}
